﻿using System;

namespace BinarySearchTree
{
    class BinarySearchTree<T> where T:IComparable
    {
        TreeNode<T> root;

        internal TreeNode<T> Root
        {
            get
            {
                return root;
            }
            set
            {
                root = value;
            }
        }
        
        public BinarySearchTree()
        {
            root = null;
        }

        public BinarySearchTree(TreeNode<T> node)
        {
            Root = node;
        }

        public void AddNode(TreeNode<T> node)
        {
            if (root == null)
            {
                root = node;
            }
            else
            {
                root.Add(node);
            }
        }

        public bool Find(T value)
        {
            TreeNode<T> iterator = Root;
            while (iterator != null)
            {
                int Compare = value.CompareTo(iterator.Data);
                if (Compare == 0) 
                {
                    return true;
                }
                if (Compare < 0)
                {
                    iterator = iterator.Left;
                }
                else
                {
                    iterator = iterator.Right;
                }
            }
            return false;
        }
    }
}